
from ROOT import settings
from django.apps import AppConfig
from django.core.cache import cache


class BasicConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'basic'

    def ready(self):
        """
        在子类中重写此方法，以便在Django启动时运行代码。
        :return:
        """
        from utils.signals import basic_signals_email   # 监听model变化时执行方法
        self.cache_logging_email()

    @staticmethod
    def cache_logging_email():
        """
        保存日志配置缓存
        :return:
        """
        from .models import EmailModel
        obj = EmailModel.objects.get_or_create(address=settings.EMAIL_HOST_USER)
        email_list = [obj[0].address]
        settings.LOGGING['handlers']['email_warning']['toaddrs'] = email_list
        settings.LOGGING['handlers']['email_error']['toaddrs'] = email_list
        cache.set("logging", settings.LOGGING)      # 将日志模块配置保存到内存缓存
        cache.set("email", email_list)      # 将收件人地址保存到内存缓存
